package com.fram.service.api.v1.android.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.android.category.request.CategoryAndroidRequest;
import com.fram.bean.admin.android.category.response.CategoryAndroidResponse;
import com.fram.bean.admin.android.category.response.CategoryWithChildrenResponse;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.DistributorProjectCategory;
import com.fram.mapper.DistributorProjectCategoryMapper;
import com.fram.mapper.ProjectCategoryMapper;
import com.fram.service.api.v1.android.GoodsCategoryAndroidService;

@Service
public class GoodsCategoryAndroidServiceImpl implements GoodsCategoryAndroidService{
	@Autowired
	private ProjectCategoryMapper projectCategoryMapper;
	@Autowired
	private DistributorProjectCategoryMapper distributorProjectCategoryMapper;
	
	@Override
	public List<CategoryAndroidResponse> listForAndroid(CategoryAndroidRequest request) throws ProException {
		List<CategoryAndroidResponse> list = distributorProjectCategoryMapper.listForAndroid(request);
		if(list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public List<CategoryWithChildrenResponse> listWithChildren(Long distributorId)throws ProException  {
		List<CategoryWithChildrenResponse> list=projectCategoryMapper.listWithChildren(distributorId);
		if(list == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}

	@Override
	public void add(DistributorProjectCategory request) throws ProException {
		try {
			if (distributorProjectCategoryMapper.insertSelective(request) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		} catch (DataAccessException e) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(),ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
	}
}
